package hikari.persistence.handlersimpl;

import com.gooddies.wiring.annotations.WiringComponent;
import hikari.objects3d.Basic3dObject;
import hikari.persistence.file.Chunk;
import hikari.persistence.file.FileSection;
import hikari.persistence.persisthandlers.IPersistantObject3dReadHandler;
import hikari.persistence.persisthandlers.PersistantHandler;
import java.io.IOException;
import java.util.List;
import org.swig.jirr.ISceneManager;
import org.swig.jirr.vector3df;

/**
 * @author Dmitry
 */
@WiringComponent(singleton = true, lazy = true)
@PersistantHandler(sectionName = "tfm")
public class TransformationLoadHandler implements IPersistantObject3dReadHandler {

    private TransformationLoadHandler() {
    }

    private vector3df readVector(FileSection section) throws IOException {
        float x = section.readFloat(section.getChunk("x"));
        float y = section.readFloat(section.getChunk("y"));
        float z = section.readFloat(section.getChunk("z"));

        vector3df vector = new vector3df(x, y, z);
        return vector;
    }

    @Override
    public Basic3dObject read(ISceneManager sceneManager, Basic3dObject object, FileSection section) throws IOException {
        String objectName = section.readString(section.getChunk("name"));
        object.setObjectName(objectName);
        List<Chunk> sectionsChunks = section.getAllSections();
        for (Chunk chunk : sectionsChunks) {
            switch (chunk.getName()) {
                case "pos":
                    vector3df pos = readVector(section.readSection(chunk));
                    object.setPosition(pos);
                    break;
                case "rot":
                    vector3df rot = readVector(section.readSection(chunk));
                    object.setRotation(rot);
                    break;
                case "scl":
                    vector3df scl = readVector(section.readSection(chunk));
                    object.setScaling(scl);
                    break;
            }
        }
        return object;
    }
}